home *** CD-ROM | disk | FTP | other *** search
- Path: camelot.dsccc.com!not-for-mail
- From: kcline@sun152.spd.dsccc.com (Kevin Cline)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ & macros
- Date: 22 Feb 1996 15:47:46 -0600
- Organization: DSC Communications Corporation Switch Products Division
- Message-ID: <4gioa2$nsb@sun152.spd.dsccc.com>
- References: <DLBxoH.GLw@ariel.cs.yorku.ca> <310EA2DE.6927@a4430edc.esr.hp.com> <4eun5f$50c@cloner4.netcom.com> <4gg06d$401m@news-s01.ny.us.ibm.net>
- NNTP-Posting-Host: sun152.spd.dsccc.com
-
- In article <4gg06d$401m@news-s01.ny.us.ibm.net>, <bbogard@ibm.net> wrote:
- >In <4eun5f$50c@cloner4.netcom.com>, swampwiz@ix.netcom.com(Jean P. Laborde ) writes:
- >>
- >>Someone responded to this group and advocated using a define macro.
- >>
- >>I thought that was extremely bad practice in C++.
- >>
- >>Comments Encouraged,
- >>The Swamp Wizard
- >
- >Using #define macros is not a bad idea, but it depends on the problem and restrictions
- >of the project you are working on. Sometime, for speed you want just a macro and not
- >have to make a function call.
-
- Using macros to define functions is a very bad idea. Use inline functions
- instead. This allows the compiler to decide whether inlining the code
- is faster than a function call. The compiler knows more about this than
- the programmer. Also, inline functions calls are typesafe.
-
- Macros should only be used for text-processing tricks that improve
- code maintainability, e.g.
-
- #define FRUIT_LIST \
- FRUIT(apple), \
- FRUIT(orange), \
- FRUIT(pear)
-
- #define FRUIT(t) t
- typedef enum { ITEM_LIST } Fruit;
- #undef FRUIT
-
- // #t produces a quoted string
- #define FRUIT(t) #t
- static const char* const FruitNameTable[] = { ITEM_LIST };
- #undef ITEM
-
- When the list gets long, techniques like this can be useful for
- reducing redundancies in source code. I find that as soon
- as I have to specify the same thing twice, and the compiler is unable
- to make sure they match, they won't.
- --
- Kevin Cline
-